home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 28
/
Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso
/
Aminet
/
dev
/
misc
/
gms_joyports.lha
/
JoyPorts
/
JOY_Init.c
next >
Wrap
Text File
|
1998-10-10
|
12KB
|
413 lines
#define JP_MOUSE 0
#define JP_JOYSTICK 1
#define JP_ANALOGUE 2
#define JP_JOYPAD 3
#define JP_SEGAPAD 4
#define JP_KEYBOARD 5
void ReadAnalogue(struct JoyData *JoyData);
void ReadJoyPad(struct JoyData *JoyData);
void ReadSegaPad(struct JoyData *JoyData);
void ReadJoystick(struct JoyData *JoyData);
void ReadKeyboard(struct JoyData *JoyData);
void ReadMouse(struct JoyData *JoyData);
/***********************************************************************************/
void ReadAnalogue(struct JoyData *JoyData)
{
ReadMouse(JoyData);
}
/***********************************************************************************/
void ReadJoyPad(struct JoyData *JoyData)
{
ReadJoystick(JoyData);
}
/***********************************************************************************/
void ReadSegaPad(struct JoyData *JoyData)
{
ReadJoystick(JoyData);
}
/************************************************************************************
** This is the joystick emulator for the Keyboard. It uses the Keyboard object
** to read the keys and compares them to the emulation keys set out by the user.
** I guess it is kind of slower than what might be preferred, but it is an emulator
** after all.
**
** BUG/HACK - Using this method the program will actually interfere with the Task's
** use of the keyboard, so we should allow for a qualifier key from GMSPrefs
** and also prevent the key buffer position from moving.
*/
void ReadKeyboard(struct JoyData *JoyData)
{
struct Keyboard *keys;
struct JoyKeys *emu;
WORD i;
WORD qual;
UBYTE value;
JoyData->XChange = NULL;
JoyData->YChange = NULL;
JoyData->ZChange = NULL;
JoyData->Buttons = NULL;
if ((keys = JoyData->prvKeys) AND (emu = JoyData->prvEmu)) {
Query(keys);
for (i = 0; i < keys->AmtRead; i++) {
qual = keys->Buffer[i].Qualifier;
if (qual & KQ_HELD) {
value = keys->Buffer[i].Value;
if (value IS emu->Left) JoyData->XChange -= 1;
else if (value IS emu->Right) JoyData->XChange += 1;
else if (value IS emu->Up) JoyData->YChange -= 1;
else if (value IS emu->Down) JoyData->YChange += 1;
else if (value IS emu->Fire1) JoyData->Buttons |= JD_FIRE1;
else if (value IS emu->Fire2) JoyData->Buttons |= JD_FIRE2;
else if (value IS emu->Fire3) JoyData->Buttons |= JD_FIRE3;
else if (value IS emu->Fire4) JoyData->Buttons |= JD_FIRE4;
else if (value IS emu->Fire5) JoyData->Buttons |= JD_FIRE5;
else if (value IS emu->Fire6) JoyData->Buttons |= JD_FIRE6;
else if (value IS emu->Fire7) JoyData->Buttons |= JD_FIRE7;
else if (value IS emu->Fire8) JoyData->Buttons |= JD_FIRE8;
}
}
}
}
/***********************************************************************************/
void ReadMouse(struct JoyData *JoyData)
{
UWORD tmp;
WORD X, Y;
LONG buttons = NULL;
/*** PORT 1 ***/
if (JoyData->Port IS 1) {
tmp = custom->joy0dat;
X = tmp & 0x00ff;
Y = (tmp>>8);
tmp = JoyData->prvOldX;
JoyData->prvOldX = X;
X -= tmp;
tmp = JoyData->prvOldY;
JoyData->prvOldY = Y;
Y -= tmp;
if (!(X > -127)) { X += 255; }
if (!(X < +127)) { X -= 255; }
if (!(Y > -127)) { Y += 255; }
if (!(Y < +127)) { Y -= 255; }
tmp = custom->potinp;
if (!(cia->ciapra & (1<<6))) buttons |= JD_LMB;
if (!(tmp & (1<<10))) buttons |= JD_RMB;
if (!(tmp & (1<<12))) buttons |= JD_MMB; /* This is a guess... */
}
/*** PORT 2 ***/
else if (JoyData->Port IS 2) {
tmp = custom->joy1dat;
X = tmp & 0x00ff;
Y = (tmp>>8);
tmp = JoyData->prvOldX;
JoyData->prvOldX = X;
X -= tmp;
tmp = JoyData->prvOldY;
JoyData->prvOldY = Y;
Y -= tmp;
if (!(X > -127)) { X += 255; }
if (!(X < +127)) { X -= 255; }
if (!(Y > -127)) { Y += 255; }
if (!(Y < +127)) { Y -= 255; }
tmp = custom->potinp;
if (!(cia->ciapra & (1<<7))) buttons |= JD_LMB;
if (!(tmp & (1<<14))) buttons |= JD_RMB; /* WRONG */
if (!(tmp & (1<<8))) buttons |= JD_MMB; /* This is a guess... */
}
/*** UNSUPPORTED PORT ***/
else {
JoyData->XChange = NULL;
JoyData->YChange = NULL;
JoyData->ZChange = NULL;
JoyData->Buttons = NULL;
return;
}
JoyData->XChange = X;
JoyData->YChange = Y;
JoyData->ZChange = NULL;
JoyData->Buttons = buttons;
}
/***********************************************************************************/
void ReadJoystick(struct JoyData *JoyData)
{
UWORD joy;
UWORD tmp;
LONG buttons = NULL;
tmp = custom->potinp;
/*** PORT 1 ***/
if (JoyData->Port IS 1) {
joy = custom->joy0dat;
if (!(cia->ciapra & (1<<6))) buttons |= JD_FIRE1;
if (!(tmp & (1<<10))) buttons |= JD_FIRE2; /* WRONG */
if (!(tmp & (1<<14))) buttons |= JD_FIRE3; /* This is a guess... */
}
/*** PORT 2 ***/
else if (JoyData->Port IS 2) {
joy = custom->joy1dat;
if (!(cia->ciapra & (1<<7))) buttons |= JD_FIRE1;
/*if (!(tmp & (1<<14)))*/ buttons |= JD_FIRE2; /* WRONG */
if (!(tmp & (1<<8))) buttons |= JD_FIRE3; /* This is a guess... */
}
/*** UNSUPPORTED PORT ***/
else {
JoyData->XChange = NULL;
JoyData->YChange = NULL;
JoyData->ZChange = NULL;
JoyData->Buttons = NULL;
return;
}
if (joy & 0x0002) JoyData->XChange = 1;
else if (joy & 0x0200) JoyData->XChange = -1;
else JoyData->XChange = NULL;
if ((joy >> 1^ joy) & 0x0001) JoyData->YChange = 1;
else if ((joy >> 1^ joy) & 0x0100) JoyData->YChange = -1;
else JoyData->YChange = NULL;
JoyData->Buttons = buttons;
}
/************************************************************************************
** Action: Free()
** Object: JoyData
*/
LIBFUNC void JOY_Free(mreg(__a0) struct JoyData *JoyData)
{
if (JoyData->prvKeys) {
Free(JoyData->prvKeys);
}
Public->OpenCount--;
}
/************************************************************************************
** Action: Get()
** Object: JoyData
*/
LIBFUNC struct JoyData * JOY_Get(mreg(__a0) struct Stats *Stats)
{
struct JoyData *JoyData;
if (JoyData = AllocMemBlock(sizeof(struct JoyData), MEM_RESOURCED|Stats->MemFlags)) {
JoyData->Head.ID = ID_JOYDATA;
JoyData->Head.Version = VER_JOYDATA;
Public->OpenCount++;
return(JoyData);
}
else {
ErrCode(ERR_FAILED);
return(NULL);
}
}
/************************************************************************************
** Action: Init()
** Object: JoyData
**
** Resets the timer and all of the values in the structure.
*/
LIBFUNC ECODE JOY_Init(mreg(__a0) struct JoyData *JoyData)
{
struct DPKTask *Task;
/* Note that we set mouse port as the system default if the
** programmer has not set a port number.
*/
if (JoyData->Port IS JPORT_ANALOGUE) {
JoyData->Port = 1;
}
else if (JoyData->Port IS JPORT_DIGITAL) {
JoyData->Port = 2;
}
else if (JoyData->Port IS NULL) {
JoyData->Port = 1;
}
else if ((JoyData->Port < 1) OR (JoyData->Port > 4)) {
DPrintF("!Init:","JoyData port number is out of range (1 - 4).");
return(ERR_DATA);
}
/* Set time-out values (micro-seconds) */
if (JoyData->ButtonTimeOut IS NULL) {
JoyData->ButtonTimeOut = 200;
}
if (JoyData->MoveTimeOut IS NULL) {
JoyData->MoveTimeOut = 200;
}
/* Set the limits. We also check that the sign of each setting
** is correct.
*/
if (JoyData->NXLimit IS NULL) JoyData->NXLimit = -100;
if (JoyData->NYLimit IS NULL) JoyData->NYLimit = -100;
if (JoyData->PXLimit IS NULL) JoyData->PXLimit = +100;
if (JoyData->PYLimit IS NULL) JoyData->PYLimit = +100;
if (JoyData->NXLimit > 0) JoyData->NXLimit = -JoyData->NXLimit;
if (JoyData->NYLimit > 0) JoyData->NYLimit = -JoyData->NYLimit;
if (JoyData->PXLimit < 0) JoyData->PXLimit = -JoyData->PXLimit;
if (JoyData->PYLimit < 0) JoyData->PYLimit = -JoyD